package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {

    @Autowired
    DtsCommentService commentService;

    @Autowired
    DtsBrandService brandService;


    @RequestMapping("/list")
    public Object list(
            @RequestParam(name="page",required=false,defaultValue="1") Integer page,
            @RequestParam(name="limit",required=false,defaultValue="20") Integer limit ,
            Integer userId ,
            Integer valueId,
            @RequestParam(name="sort",required=false,defaultValue="add_time") String sort,
            @RequestParam(name="order",required=false,defaultValue="desc") String order) {

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
        //是否是品牌制造商
        if(subject.hasRole("品牌制造商")){
            brandIds = brandService.findBrandIdsByAdminId(admin.getId());
        }

        IPage<DtsComment> ipage = commentService.findPage(page, limit, userId, valueId, sort, order, brandIds);

        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords()) ;
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        commentService.deleteById(comment.getId());

        return ResponseUtil.ok();
    }
}
